<?php

namespace App\Http\Controllers\admin;

use App\Http\Model\Brand;
use App\Http\Model\Categroy;
use App\Http\Model\Goods;
use App\Http\Model\Unit;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

/**
 * Class GoodsController 后台商品控制器
 * @package App\Http\Controllers\admin
 */
class GoodsController extends AdminPlatformController
{
    /** 属性声明 */
    protected $default_goods_ori;
    //每页显示5条商品记录
    protected $pagesize = 6;

    /** 方法声明 */
    /**
     * 构造方法
     */
    public function __construct(Route $route){
        parent::__construct($route);
        $this -> default_goods_ori = "/admin/Images/system/goods_default.png";
    }
    /**
     * get 展示所有商品列表
     */
    public function index(){
        //获取所有商品信息
        $data = Goods::where('is_delete',0)
            ->orderBy('add_time','desc')
            ->paginate($this -> pagesize);
        //获取商品栏目
        $category = Categroy::all();
        $cat = new Categroy();
        $cat = $cat->getTree($category);
        //获取商品单位
        $unit = Unit::all();
        //获取商品名称
        $brand = Brand::all();
        //dd($cat,$brand,$unit);

        //加载商品列表视图
        return view('admin.goods.goodsList',compact('data','cat','brand','unit'));
    }

    /**
     * get 添加商品视图加载
     */
    public function create(){
        //加载添加商品视图
        //获取商品栏目
        $category = Categroy::all();
        $cat = new Categroy();
        $cat = $cat->getTree($category);
        //获取商品单位
        $unit = Unit::all();
        //获取商品名称
        $brand = Brand::all();
        //dd($cat,$brand,$unit);
        return view('admin.goods.goodsAdd',compact('cat','unit','brand'));
    }

    /**
     * post 处理添加商品数据
     */
    public function store(){
        //数据验证
        $input = Input::except('MAX_FILE_SIZE','_token');
        $rule = [
            'goods_name' => 'required',
            //'cat_id' => 'numeric|not in:0',
            'goods_number' => 'required|numeric',
            'market_price' => 'required|numeric',
            'shop_price' => 'required|numeric',
        ];
        $message = [
            'goods_name.required' => '商品名必填!',
            'cat_id.numeric' => '栏目id必须整数值!',
            'goods_number.required' => '商品数量必填!',
            'market_price.required' => '商品价格必填!',
            'shop_price.required' => '本店售价必填!'
        ];

        $validate = Validator::make($input,$rule,$message);
        if($validate->fails()){
            return back() -> withErrors($validate)->withInput();
        }
        //生成货号
        $input['goods_sn'] = empty($input['goods_sn']) ?  $this -> getSn() : $input['goods_sn'];
        //记录添加商品时间
        $input['add_time'] = time();
        //记录商品原图信息
        $input['goods_ori'] = empty($input['goods_ori']) ?
            $this -> default_goods_ori : $input['goods_ori'];
        //记录商品精品 热销 新品 信息
        if(isset($input['goods_state'])){
            $status = array_values($input['goods_state']);
            $input['is_new'] = in_array(1,$status) ? 1 : 0;
            $input['is_best'] = in_array(2,$status) ? 1 : 0;
            $input['is_hot'] = in_array(3,$status) ? 1 : 0;
        }
        //商品状态数据
        unset($input['goods_state']);
        //存储数据
        if(Goods::insert($input)){
            //跳转商品列表
            return redirect('manager/goods');
        }
        //失败跳回商品添加页
        return back() -> with('errors','商品添加失败,请稍后再试!');
    }

    /**
     * 生成商品货号 货号格式:15位 20160206 + rand(7)
     * @return string 返回15位随机货号
     */
    private function getSn(){
         return date('Ymd') . mt_rand(1000000,9999999);
    }

    /**
     * get 修改商品视图加载
     * @param int $gid 商品主键id
     * @return void
     */
    public function edit($gid){
        //获取商品信息
        $goods_info = Goods::find($gid);
        //获取商品栏目
        $category = Categroy::all();
        $cated = new Categroy();
        $cats = $cated->getTree($category);
        //获取商品单位
        $units = Unit::all();
        //获取商品名称
        $brands = Brand::all();
        //dd($cat,$brand,$unit);
        //加载编辑商品视图
        return view('admin.goods.goodsEdit',compact('goods_info','cats','brands','units'));
    }

    /**
     * get 商品回收站视图加载
     */
    public function show(){
        //获取回收站商品信息
        $goods_list = Goods::where('is_delete',1)->paginate($this->pagesize);
        return view('admin.goods.goodsRecycle',compact('goods_list'));
    }

    /**
     * 将商品加入回收站
     */
    public function recycle($gid){
        //执行更改商品状态
        $goods = Goods::find($gid);
        $goods -> is_delete = 1;
        $goods -> save();
        //跳转商品列表
        return redirect('manager/goods');
    }

    /**
     * put 执行修改商品数据
     * @param int $gid 商品主键id
     * @return void
     */
    public function update($gid){
        $input = Input::except('MAX_FILE_SIZE','_token','_method');
        //记录商品精品 热销 新品 信息
        if(isset($input['goods_state'])){
            $status = array_values($input['goods_state']);
            $input['is_new'] = in_array(1,$status) ? 1 : 0;
            $input['is_best'] = in_array(2,$status) ? 1 : 0;
            $input['is_hot'] = in_array(3,$status) ? 1 : 0;
        }
        //商品状态数据
        unset($input['goods_state']);

        $rule = [
            'goods_name' => 'required',
            //'cat_id' => 'numeric|not in:0',
            'goods_number' => 'required|numeric',
            'market_price' => 'required|numeric',
            'shop_price' => 'required|numeric',
        ];
        $message = [
            'goods_name.required' => '商品名必填!',
            'cat_id.numeric' => '栏目id必须整数值!',
            'goods_number.required' => '商品数量必填!',
            'market_price.required' => '商品价格必填!',
            'shop_price.required' => '本店售价必填!'
        ];

        $validate = Validator::make($input,$rule,$message);
        if($validate->passes()){
            $rs = Goods::where('goods_id',$gid)->update($input);
            if($rs){
                return redirect('manager/goods');
            }else{
                return back()->with('errors','编辑失败稍后再试');
            }
        }else{

            return back()->withErrors($validate)->withInput();
        }

    }

    /**
     * delete 商品搜索
     * @param int $gid 商品主键id
     * @return void
     */
    public function search(){
        $input = Input::except('_token');


    }

    /**
     * delete 删除商品
     * @param int $gid 商品主键id
     * @return void
     */
    public function destroy($gid){

    }
}
